<?php
    // This is used to delete artifacts
    // INPUT: $_POST[]
    //
    // OUTPUT: Return text passed (encoded via htmlspecialchars)
    // SIDE EFFECT: Store _POST in database after authenticating user permissions


    require_once("../../../includes.php");

    require_once("../model/resource.php");
    require_once("../model/artifact.php");
    require_once("../model/page.php");
    require_once("../model/decoration.php");

    if ( !isloggedin() ) {
        trigger_error('You mus be logged in to use this page', E_USER_ERROR);
    }

    // Orienting data
    $artifact_ident = intval( required_param('artifact_ident' ) );
    $artifact = folio_artifact::SelectWhereArtifactIdent( $artifact_ident );

    // Validate permissions
    $page = folio_page::SelectWherePageIdent( $artifact->page_ident );

    if ( !$page->Permission('update') ) {
        trigger_error('You do not have permission to edit this page', E_USER_ERROR);
    }

    if ( folio_artifact::Delete( $artifact_ident ) ) {
        // Successfull
         echo <<< END
        <!-- JS to reset sort-->
            <script type="text/javascript">
            // <![CDATA[
                folio_RichEditor_SetOrder( {$page->page_ident} );
            // ]]>
            </script>
END;
    } else {
        // Failure
        echo '<p>Error: Unable to delete artifact ' . $artifact_ident . '</p>';
    }
?>
